//
//  Date+Extension.swift
//  WidgetExample
//
//  Created by Ben on 2022/4/7.
//  Copyright © 2022 Ben. All rights reserved.
//

import Foundation

extension Date {
    
    // 返回当前时间戳，毫秒级Int类型
     static var currTime: Int {
         return Int(Date().timeIntervalSince1970 * 1000)
     }
    
    /// 返回当前时间戳，毫秒字符串
    static var currTimeStr: String {
        return "\(currTime)"
    }
    
    /// 时间戳格式化为时间字符串, 默认格式yyyy-MM-dd HH:mm:ss
    ///
    /// - Parameters:
    ///   - date: 时间戳，Double类型
    ///   - dateFormat: 格式
    /// - Returns: 返回格式化后的时间字符串
    static func formatDateToStr(timeInterval: TimeInterval, dateFormat: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = dateFormat
        let dateStr = dateFormatter.string(from: Date.init(timeIntervalSince1970: timeInterval))
        return dateStr
    }
    
    var hour: Int {
        return getTimeInfo().hour
    }
    var min: Int {
        return getTimeInfo().min
    }
    
    func getTimeInfo() -> (hour: Int, min: Int, second: Int, week: String) {
        let calendar = Calendar.current
        
        let hour = calendar.component(.hour, from: self)
        let min = calendar.component(.minute, from: self)
        let second = calendar.component(.second, from: self)
        let week = calendar.component(.weekday, from: self)
        
        var weekStr = ""
        switch week {
        case 1:
            weekStr = "日"
        case 2:
            weekStr = "一"
        case 3:
            weekStr = "二"
        case 4:
            weekStr = "三"
        case 5:
            weekStr = "四"
        case 6:
            weekStr = "五"
        case 7:
            weekStr = "六"
        default:
            ()
        }
        return (hour, min, second, weekStr)
    }
}
